#!/usr/bin/env php
<?php
function proc($conn)
{/*{{{*/
    $rs=mssql_query("select empno,convert(varchar,update_ts,20),name ".
            "from my_emp where empno<100", $conn);
    if($rs === false)
    {
        printf("select fail\n");
        return false;
    }
    printf("num: %d\n", mssql_num_rows($rs));
    while($row=mssql_fetch_array($rs))
    {
        $datetime=$row[1];
        $datetime=str_replace(' ', '',
                str_replace(':', '', str_replace('-', '', $datetime)));
        printf("%d %s %s\n", $row[0], $datetime, $row[2]);
    }
    mssql_free_result($rs);

    $rs=mssql_query("update my_emp set update_ts=getdate(),age=30 ".
            "where empno=1", $conn);
    if($rs === false)
    {
        printf("update fail\n");
        return false;
    }
    if(mssql_rows_affected($conn) == 0)
        printf("update no row\n");
    else
        printf("update succeed\n");

    return true;
}/*}}}*/

function main()
{/*{{{*/
    $conn=mssql_connect('test', 'dbuser', 'dbuser');
    if($conn === false)
    {
        printf("connect to server fail\n");
        return 1;
    }
    if(mssql_select_db('test', $conn) == false)
    {
        printf("select db(test) fail\n");
        return 1;
    }

    $rtn=0;
    if(proc($conn) === false)
        $rtn=1;

    mssql_close($conn);

    return $rtn;
}/*}}}*/

$rtn=main();
exit($rtn);
?>
